package ItemSystem;

import org.newdawn.slick.Input;
import org.newdawn.slick.geom.Vector2f;

import entity.Player;

public class RangedWeapon extends Item
{
	private int ammoMax;
	private int currentAmmo;
	
	private int clipSize;
	private int ammoInClip;
	
	private int damage;
	
	public RangedWeapon(Vector2f position, String itemName, int damage, int currentAmmo, int ammoMax, int clipSize) 
	{
		super(position, itemName);
		this.setDamage(damage);
		this.currentAmmo = currentAmmo;
		this.ammoMax = ammoMax;
		this.clipSize = clipSize;
		this.ammoInClip = clipSize;
	}


	public void attack(Input userInput) 
	{
		if(ammoInClip > 0)
		{
			ammoInClip--;
			
			Vector2f bulletPos = new Vector2f(getPosition().getX() + 8, getPosition().getY() + 8);
			float mouseY = userInput.getMouseY();
			float changeToX;
			float changeToY;
			float newHypot = 16;
			
			if(mouseY != getPosition().getY())
			{
				bulletPos.set(bulletPos.getX() + 4, bulletPos.getY());
			}
			
			changeToY = (float) (newHypot * Math.sin(Math.toRadians(getRotation() + 90)));
			changeToX = (float) (newHypot * Math.cos(Math.toRadians(getRotation() + 90)));
			
			bulletPos.set(bulletPos.getX() - changeToX, bulletPos.getY() - changeToY);
		}
	}


	public void update(Player user) 
	{
		super.update(user);
		
		if (user.getInput().isMousePressed(Input.MOUSE_LEFT_BUTTON))
		{
			attack(user.getInput());
		}
		
		if (user.getInput().isKeyPressed(Input.KEY_R))
		{
			reload();
		}
		
	}

	private void reload() 
	{
		if(currentAmmo > clipSize)
		{
			currentAmmo -= clipSize;
			ammoInClip = clipSize;
		}
		else
		{
			ammoInClip = currentAmmo;
			currentAmmo = 0;
		}
	}
	

	public void addAmmo(int amount)
	{
		if(currentAmmo + amount < ammoMax)
		{
			currentAmmo += amount;
		}
		else
		{
			currentAmmo = ammoMax;
		}
	}
	
	public int getAmmoMax() 
	{
		return ammoMax;
	}

	public int getCurrentAmmo() 
	{
		return currentAmmo;
	}

	public void setCurrentAmmo(int currentAmmo) 
	{
		this.currentAmmo = currentAmmo;
	}

	public int getClipSize() 
	{
		return clipSize;
	}

	public void setClipSize(int clipSize)
	{
		this.clipSize = clipSize;
	}

	public int getAmmoInClip() 
	{
		return ammoInClip;
	}

	public void setAmmoInClip(int ammoInClip) 
	{
		this.ammoInClip = ammoInClip;
	}


	public int getDamage() 
	{
		return damage;
	}


	public void setDamage(int damage) 
	{
		this.damage = damage;
	}
	
	
	
}
